1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.google;
18  
19  import static com.google.common.collect.testing.features.CollectionSize.ONE;
20  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
21  
22  import com.google.common.annotations.GwtCompatible;
23  import com.google.common.collect.HashMultiset;
24  import com.google.common.collect.Multiset;
25  import com.google.common.collect.Multisets;
26  import com.google.common.collect.testing.features.CollectionSize;
27  
28  /**
29   * A generic JUnit test which tests multiset-specific read operations.
30   * Can't be invoked directly; please see
31   * {@link com.google.common.collect.testing.SetTestSuiteBuilder}.
32   *
33   * @author Jared Levy
34   */
35  @GwtCompatible
36  public class MultisetReadsTester<E> extends AbstractMultisetTester<E> {
37  
38    @CollectionSize.Require(absent = ZERO)
39    public void testElementSet_contains() {
40      assertTrue("multiset.elementSet().contains(present) returned false",
41          getMultiset().elementSet().contains(samples.e0));
42    }
43  
44    @CollectionSize.Require(absent = ZERO)
45    public void testEntrySet_contains() {
46      assertTrue("multiset.entrySet() didn't contain [present, 1]",
47          getMultiset().entrySet().contains(
48              Multisets.immutableEntry(samples.e0, 1)));
49    }
50  
51    public void testEntrySet_contains_count0() {
52      assertFalse("multiset.entrySet() contains [missing, 0]",
53          getMultiset().entrySet().contains(
54              Multisets.immutableEntry(samples.e3, 0)));
55    }
56  
57    public void testEntrySet_contains_nonentry() {
58      assertFalse("multiset.entrySet() contains a non-entry",
59          getMultiset().entrySet().contains(samples.e0));
60    }
61  
62    public void testEntrySet_twice() {
63      assertEquals("calling multiset.entrySet() twice returned unequal sets",
64          getMultiset().entrySet(), getMultiset().entrySet());
65    }
66  
67    @CollectionSize.Require(ZERO)
68    public void testEntrySet_hashCode_size0() {
69      assertEquals("multiset.entrySet() has incorrect hash code",
70          0, getMultiset().entrySet().hashCode());
71    }
72  
73    @CollectionSize.Require(ONE)
74    public void testEntrySet_hashCode_size1() {
75      assertEquals("multiset.entrySet() has incorrect hash code",
76          1 ^ samples.e0.hashCode(), getMultiset().entrySet().hashCode());
77    }
78  
79    public void testEquals_yes() {
80      assertTrue("multiset doesn't equal a multiset with the same elements",
81          getMultiset().equals(HashMultiset.create(getSampleElements())));
82    }
83  
84    public void testEquals_differentSize() {
85      Multiset<E> other = HashMultiset.create(getSampleElements());
86      other.add(samples.e0);
87      assertFalse("multiset equals a multiset with a different size",
88          getMultiset().equals(other));
89    }
90  
91    @CollectionSize.Require(absent = ZERO)
92    public void testEquals_differentElements() {
93      Multiset<E> other = HashMultiset.create(getSampleElements());
94      other.remove(samples.e0);
95      other.add(samples.e3);
96      assertFalse("multiset equals a multiset with different elements",
97          getMultiset().equals(other));
98    }
99  
100   @CollectionSize.Require(ZERO)
101   public void testHashCode_size0() {
102     assertEquals("multiset has incorrect hash code",
103         0, getMultiset().hashCode());
104   }
105 
106   @CollectionSize.Require(ONE)
107   public void testHashCode_size1() {
108     assertEquals("multiset has incorrect hash code",
109         1 ^ samples.e0.hashCode(), getMultiset().hashCode());
110   }
111 }